/*
 * Copyright (c) 2021 Talkweb Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "infrared_demo.h"

void InfraredSensorInit(void)
{
    IoTGpioInit(IOT_GPIO_IO_GPIO_7); 
    IoTIoSetFunc(IOT_GPIO_IO_GPIO_7, IOT_IO_FUNC_GPIO_7_GPIO);
    IoTIoSetPull(IOT_GPIO_IO_GPIO_7, IOT_IO_PULL_DOWN);
    IoTGpioSetDir(IOT_GPIO_IO_GPIO_7,IOT_GPIO_DIR_IN);
}

IotGpioValue GetInfraredSensorVal(void)
{
    IotGpioValue gpioValue = IOT_GPIO_VALUE0;
    IoTGpioGetInputVal(IOT_GPIO_IO_GPIO_7, &gpioValue);

    return gpioValue;
}

enum ENV_BODY_STATE GetEnvBodyState(void)
{
    static enum ENV_BODY_STATE  bodyState = BODY_NOT;  

    //红外检测到有人 
    if(GetInfraredSensorVal() == IOT_GPIO_VALUE1)
    {
        printf("have body \r\n");
        switch(bodyState)
        {
            case BODY_NOT:  bodyState = BODY_COME; break;
            case BODY_COME: bodyState = BODY_KEEP; break;
            case BODY_KEEP: bodyState = BODY_KEEP; break; 
            case BODY_LEAVE:bodyState = BODY_COME; break;               
            default:        bodyState = BODY_KEEP; break;
        }
    }

    //红外检测到无人
    else
    {
        printf("not body \r\n");
        switch(bodyState)
        {
            case BODY_NOT:  bodyState = BODY_NOT;  break;
            case BODY_COME: bodyState = BODY_LEAVE;break;
            case BODY_KEEP: bodyState = BODY_LEAVE;break; 
            case BODY_LEAVE:bodyState = BODY_NOT;  break;               
            default:        bodyState = BODY_LEAVE;break;
        }
    }

    return bodyState;
}


